package com.fyales.android.component;

import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author fyales
 * @date 5/2/15
 */
public class PullToRefreshLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {

    public static final int STATUS_IDLE = 0;
    public static final int STATUS_REFRESHING = 1;

    private ListView listView;
    private Context context;
    private View footerView;
    private ProgressBar footerProgress;
    private TextView footerLabelTv;
    private ActionListener listener;

    private int mStatus;
    private boolean mIsHasMore;

    public PullToRefreshLayout(Context context) {
        super(context);
    }

    public PullToRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        mStatus = STATUS_IDLE;
        mIsHasMore = true;


        View view = LayoutInflater.from(this.context).inflate(R.layout.pull_to_refresh_layout, null);
        this.addView(view);
        listView = (ListView) view.findViewById(R.id.refresh_lv);
        listView.setOnScrollListener(this);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    listener.itemClick(position);
                }
            }
        });

        footerView = LayoutInflater.from(this.context).inflate(R.layout.refresh_footer_view, null);
        footerProgress = (ProgressBar) footerView.findViewById(R.id.footer_progress);
        footerLabelTv = (TextView) footerView.findViewById(R.id.footer_label_tv);
    }

    public void setRefreshAndLoadMoreListener(ActionListener listener) {
        this.listener = listener;
    }

    public void setmIsHasMore(boolean mIsHasMore) {
        this.mIsHasMore = mIsHasMore;
        if (!mIsHasMore) {
            listView.addFooterView(footerView);
            footerProgress.setVisibility(View.GONE);
            footerLabelTv.setText("没有更多内容了");
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (totalItemCount > 0 && firstVisibleItem + visibleItemCount == totalItemCount && mStatus == STATUS_IDLE && mIsHasMore) {
            loadMore();
        }
        return;
    }


    private void loadMore() {
        mStatus = STATUS_REFRESHING;
        listView.addFooterView(footerView);
        if (listener != null) {
            listener.loadMore();
        }
    }

    public void loadFinished() {
        listView.removeFooterView(footerView);
        mStatus = STATUS_IDLE;
    }

    public void setAdapter(BaseAdapter adapter) {
        listView.setAdapter(adapter);
    }


    public interface ActionListener {
        void itemClick(int position);

        void loadMore();
    }


}
